package com.fulihui.information.biz.conv;

import com.fulihui.information.dal.dataobj.InfoDislike;
import com.fulihui.information.dto.DislikeDTO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * 举报信息装换器
 * @author LeeSon on 2016/12/28 0028.
 */
public class DislikeConv {
    public static DislikeDTO toDTO(InfoDislike dislike) {
        if (dislike == null) {
            return null;
        }
        DislikeDTO dto = new DislikeDTO();
        dto.setId(dislike.getId());
        dto.setUserId(dislike.getUserId());
        dto.setContentId(dislike.getContentId());
        dto.setStatus(dislike.getStatus());
        dto.setType(dislike.getType());
        dto.setTitle(dislike.getTitle());
        dto.setNickName(dislike.getNickName());
        if (StringUtils.isNotBlank(dislike.getComment())) {
            try {
                dto.setComment(new String(Base64.decodeBase64(dislike.getComment()), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException();
            }
        }
        dto.setIp(dislike.getIp());
        dto.setOperator(dislike.getOperator());
        dto.gmtCreate(dislike.getGmtCreate());
        dto.gmtModified(dislike.getGmtModified());
        return dto;
    }

    public static List<DislikeDTO> toList(List<InfoDislike> list) {
        if (isEmpty(list)) {
            return emptyList();
        }
        return list.stream().map(DislikeConv::toDTO).collect(Collectors.toList());
    }
}